package com.ItemsRent.action;

import org.apache.struts2.ServletActionContext;

import com.ItemsRent.IService.ICustomer;
import com.ItemsRent.exception.MyHttpException;
import com.ItemsRent.model.Customer;
import com.opensymphony.xwork2.ActionContext;

public class LoginAction {
	private String stuNumber;
	private String password;
	
	private Customer customer;
	/**
	 * 定义接口cuService，接收Spring注入的实现该接口的实体类
	 */
	private ICustomer cuService;
	
	public ICustomer getCuService() {
		return cuService;
	}
	public void setCuService(ICustomer cuService) {
		this.cuService = cuService;
	}
	/*private CustomerServiceImpl cuService;
	public CustomerServiceImpl getCuService() {
		return cuService;
	}
	public void setCuService(CustomerServiceImpl cuService) {
		this.cuService = cuService;
	}*/
	public String getStuNumber() {
		return stuNumber;
	}
	public void setStuNumber(String stuNumber) {
		this.stuNumber = stuNumber;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public String login() throws Exception{
		customer = cuService.getCustomer(stuNumber, password);
		if(customer!=null){
			if(customer.getCustomerStatus()!=1){
				throw new MyHttpException("你已被禁止登录");
			}
			ServletActionContext.getRequest().getSession().setAttribute("customer", customer);
			ServletActionContext.getRequest().getSession().setAttribute("cuNumber", customer.getNumber());
			ActionContext.getContext().getSession().put(stuNumber, customer);
			System.out.println("登陆成功："+stuNumber);
			return "success_allow";
		}
		else {
			throw new MyHttpException("密码或用户名错误");
			//System.out.println("登陆失败："+stuNumber);
			//return "input";
		}
	}

}
